package com.demo.logistics.utils;

import java.util.HashMap;
import java.util.HashSet;

public class TypeConversion {
	private static final HashMap<Class<?>, String> WRAPPER_TYPES = getWrapperTypes();

    public static boolean isWrapperType(Class<?> clazz)
    {
        return WRAPPER_TYPES.containsKey(clazz);
    }
    
    public static String getWrapperType(Class<?> clazz){
    	
    	if(WRAPPER_TYPES.containsKey(clazz)){
    		return WRAPPER_TYPES.get(clazz);
    	}
    	return "n/a";
    }

    private static HashMap<Class<?>, String> getWrapperTypes()
    {
        HashMap<Class<?>, String> ret = new HashMap<Class<?>, String>();
        ret.put(Boolean.class, "boolean");
        ret.put(Character.class, "char");
        ret.put(Byte.class, "byte");
        ret.put(Short.class, "short");
        ret.put(Integer.class, "int");
        ret.put(Long.class, "long");
        ret.put(Float.class, "float");
        ret.put(Double.class, "double");
        ret.put(Void.class, "void");
        return ret;
    }
    
    public static String convertObjectToString(Object x){
    	String myString = null;
    	if(getWrapperType(x.getClass()).equals("boolean")){
    		boolean temp = (Boolean) x;
    		myString = Boolean.toString(temp);
    	}
    	else if(getWrapperType(x.getClass()).equals("char")){
    		char temp = (Character) x;
    		myString = Character.toString(temp);
    	}
    	else if(getWrapperType(x.getClass()).equals("byte")){
    		byte temp = (Byte) x;
    		myString = Byte.toString(temp);
    	}
    	else if(getWrapperType(x.getClass()).equals("short")){
    		short temp = (Short) x;
    		myString = Short.toString(temp);
    	}
    	else if(getWrapperType(x.getClass()).equals("int")){
    		int temp = (Integer) x;
    		myString = Integer.toString(temp);
    	}
    	else if(getWrapperType(x.getClass()).equals("long")){
    		long temp = (Long) x;
    		myString = Long.toString(temp);
    	}
    	else if(getWrapperType(x.getClass()).equals("double")){
    		double temp = (Double) x;
    		myString = Double.toString(temp);
    	}
    	else if(getWrapperType(x.getClass()).equals("float")){
    		float temp = (Float) x;
    		myString = Float.toString(temp);
    	}
    	else if(getWrapperType(x.getClass()).equals("void")){
    		myString = "";
    	}
    	else{
    		myString = x.toString();
    	}
    	return myString;
    }
}
